import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Designer {
    public static  void designerMode(){
        JFrame frame = new JFrame("设计师模式 - 同伴的考验");
        frame.setSize(350,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new GridLayout(8,3,10,5));
        frame.setVisible(true);
        //参数设置
        JLabel rowLabel = new JLabel("行数:");
        JTextField rowField = new JTextField("10");
        JLabel colLabel = new JLabel("列数:");
        JTextField colField = new JTextField("10");
        JLabel tntLabel = new JLabel("地雷数量:");
        JTextField tntField = new JTextField("10");
        JButton startButton = new JButton("确认并设计");
        //
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try{
                    int rows=Integer.parseInt(rowField.getText());
                    int cols=Integer.parseInt(colField.getText());
                    int tntNums=Integer.parseInt(tntField.getText());
                    if (rows<=0 || cols<=0 ||tntNums<=0){
                        if (rows<=0 || cols<=0 ||tntNums<=0){
                            JOptionPane.showMessageDialog(frame,"这样的设计是……对的吗？\n" +
                                    "已为您自动选择初始设置的10×10 - 10雷","来自系统的提问", JOptionPane.INFORMATION_MESSAGE);
                            designer(10,10,10);
                        }
                    }
                    designer(rows,cols,tntNums);
                }catch (NumberFormatException ex){
                    ///错误提示
                    JOptionPane.showMessageDialog(frame,"这样的设计是……对的吗？\n" +
                            "已为您自动选择初始设置的10×10 - 10雷","来自系统的提问", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        frame.add(rowLabel);
        frame.add(rowField);
        frame.add(colLabel);
        frame.add(colField);
        frame.add(tntLabel);
        frame.add(tntField);
        frame.add(startButton);
        }
        public static void designer(int rows,int cols,int tntNums){
            JFrame designerFrame = new JFrame("设计师模式 - 来自同伴的考验");
            designerFrame.setSize(1000,700);
            designerFrame.setLocationRelativeTo(null);
            designerFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            DesignerPanel panel = new DesignerPanel(rows,cols,tntNums);
            designerFrame.setContentPane(panel);
            designerFrame.setVisible(true);
    }
}
